package com.hzy.yiyaoba.ui;

import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzy.yiyaoba.R;
import com.hzy.yiyaoba.R.id;
import com.hzy.yiyaoba.bean.LoreShow;
import com.hzy.yiyaoba.utils.Config.LoreConfig;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.util.LogUtils;

//api.yi18.net/lore/show 知识信息详细

public class LoreDetailActivity extends BaseActivity implements OnClickListener {

	public static final String TAG = LoreDetailActivity.class.getSimpleName();
	private int mLoreId;
	private View mBackArea;
	private String mImgUrl;
	private TextView mTitleText;
	private ImageView mImage;
	private TextView mContentText;
	private View mMainArea;
	private View mProgress;
	private LoreShow mLoreShow;

	@Override
	protected int getLayoutRes() {
		return R.layout.activity_lore_detail;
	}

	private void updateMainShow(View v) {
		showView(v, mMainArea);
		showView(v, mProgress);
	}

	@Override
	protected void initEnvironment() {
		mLoreId = getIntent().getIntExtra(LoreConfig.EXTRA_LORE_ID, 0);
		mImgUrl = getIntent().getStringExtra(LoreConfig.EXTRA_IMAGE_URL);
	}

	@Override
	protected void initView() {
		mBackArea = findViewById(id.common_activity_left_area);
		mTitleText = (TextView) findViewById(id.lore_detail_title);
		mImage = (ImageView) findViewById(id.lore_detail_image);
		mContentText = (TextView) findViewById(id.lore_detail_content);
		mMainArea = findViewById(id.lore_detail_main_area);
		mProgress = findViewById(id.lore_detail_progress);

		mBackArea.setOnClickListener(this);
		mImage.setOnClickListener(this);
		downloadLoreShow();
	}

	private void updateMainView() {
		// TODO Auto-generated method stub
		if (mLoreShow != null) {
			mTitleText.setText(mLoreShow.getTitle());
			if(TextUtils.isEmpty(mImgUrl)){
				mImage.setVisibility(View.GONE);
			}else{
				mBmpUtils.display(mImage, mImgUrl);
			}
			mContentText.setText(Html.fromHtml(mLoreShow.getMessage()));
		}
		updateMainShow(mMainArea);
	}

	private void downloadLoreShow() {
		updateMainShow(mProgress);
		RequestParams params = LoreShow.createRequestParams(mLoreId);
		mHttpUtils.send(HttpMethod.POST, LoreConfig.URL_SHOW, params, new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				// TODO Auto-generated method stub
				// LogUtils.i(responseInfo.result);
				try {
					mLoreShow = LoreShow.parseLoreShow(responseInfo.result);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				updateMainView();
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				// TODO Auto-generated method stub

			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case id.common_activity_left_area:
			finish();
			break;

		case id.lore_detail_image:
			performDetailImageClick();
			break;
			
		default:
			break;
		}
	}

	private void performDetailImageClick() {
		// TODO Auto-generated method stub
		Intent i = new Intent(this, PhotoDetailActivity.class);
		i.putExtra(LoreConfig.EXTRA_IMAGE_URL, mImgUrl);
		startActivity(i);
	}

}
